﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using _4mvcBlog.Core.ViewEngine;
using BlogEngine.Core;
using System.Globalization;
using System.Threading;
using _4mvcBlog.Core.ModelBinders;

namespace _4mvcBlog
{
    public class _4mvcBlogApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            ///在程序启动的时候注册我们定义的Route规则
            RegisterRoutes(RouteTable.Routes);

            ///初始化ViewEngine
            InitViewEngine();

            ///注册ModelBinders
            RegisterModelBinders();
        }

        /// <summary>
        /// 注册路由规则
        /// </summary>
        /// <param name="routes"></param>
        public static void RegisterRoutes(RouteCollection routes)
        {
            //忽略对.axd文件的Route，也就是和WebForm一样直接去访问.axd文件
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Admin",
                "Admin/{action}/{id}",
                new { controller = "Admin", action = "Index", id = "" }
            );

            routes.MapRoute(
                "PostById",
                "Post/{id}",
                new { controller = "Home", action = "Post", id = "" },
                new { id = @"[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}" }//GUID
            );

            routes.MapRoute(
                "PostBySlug",
                "Post/{slug}",
                new { controller = "Home", action = "PostBySlug", slug = "" }
            );

            routes.MapRoute(
                "CategoryById",
                "Category/{id}",
                new { controller = "Home", action = "Category", id = "" },
                new { id = @"[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}" }//GUID
            );

            routes.MapRoute(
                "CategoryBySlug",
                "Category/{slug}",
                new { controller = "Home", action = "CategoryBySlug", slug = "" }
            );

            routes.MapRoute(
                "Default",                                              // Route 的名称
                "{controller}/{action}/{id}",                           // 带有参数的URL
                new { controller = "Home", action = "Index", id = "" }  // 设置默认的参数
            );

        }

        /// <summary>
        /// 初始化ViewEngine
        /// </summary>
        private void InitViewEngine()
        {
            //IIS7的Integrated mode不能访问HttpContext.Current，
            //只好出此下策
            BlogEngine.Core.Utils.UnCheckMobile = true;
            string theme = BlogEngine.Core.BlogSettings.Instance.Theme;
            BlogEngine.Core.Utils.UnCheckMobile = false; ;
            if (string.IsNullOrEmpty(theme))
                theme = "Default";

            ViewEngines.Engines.Clear();
            //在RC中目前还没找到好的在Controller中修改ViewEngine的办法
            //只好在这里将顺序换一下，对性能有一点影响
            //ViewEngines.Engines.Add(new ThemeViewEngine(theme));
            ViewEngines.Engines.Add(new AdminViewEngine());
            ViewEngines.Engines.Add(new ThemeViewEngine(theme));
        }

        /// <summary>
        /// 注册ModelBinders
        /// </summary>
        private static void RegisterModelBinders()
        {
            ModelBinderDictionary binders = System.Web.Mvc.ModelBinders.Binders;

            binders[typeof(Post)] = new PostModelBinder();
        }

        /// <summary>
        /// 根据设置设定当前的区域语言.
        /// 在IIS7中，这个会出错，所以写到ActionFilter中去
        /// </summary>
        void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(BlogSettings.Instance.Culture))
            {
                if (!BlogSettings.Instance.Culture.Equals("Auto"))
                {
                    CultureInfo culture = CultureInfo.CreateSpecificCulture(BlogSettings.Instance.Culture);
                    Thread.CurrentThread.CurrentUICulture = culture;
                    Thread.CurrentThread.CurrentCulture = culture;
                }
            }
        }
    }
}